package com.meituan.catering.management.order.biz.model.converter;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.meituan.catering.management.order.dao.model.CateringOrderItemAccessoryDO;

import java.util.List;
import java.util.Map;

/**
 * @author Believe
 * Date 2022/6/7 14:36
 */
public class AdjustCateringAccessoryFilter {
    public static List<CateringOrderItemAccessoryDO> accessoryDOUpdateFilter(List<CateringOrderItemAccessoryDO> accessoryDOList,
                                                                             List<CateringOrderItemAccessoryDO> queryAccessory) {
        List<CateringOrderItemAccessoryDO> accessoryList = Lists.newLinkedList();
        Map<Long, List<CateringOrderItemAccessoryDO>> listMap = Maps.newHashMap();
        for (CateringOrderItemAccessoryDO cateringOrderItemAccessoryDO : queryAccessory) {
            if (listMap.containsKey(cateringOrderItemAccessoryDO.getProductAccessoryId())) {
                List<CateringOrderItemAccessoryDO> cateringOrderItemDOS = listMap.get(cateringOrderItemAccessoryDO.getProductAccessoryId());
                cateringOrderItemDOS.add(cateringOrderItemAccessoryDO);
            } else {
                List<CateringOrderItemAccessoryDO> cateringOrderItemDOS = Lists.newLinkedList();
                cateringOrderItemDOS.add(cateringOrderItemAccessoryDO);
                listMap.put(cateringOrderItemAccessoryDO.getProductAccessoryId(), cateringOrderItemDOS);
            }
        }
        for (CateringOrderItemAccessoryDO cateringOrderItemAccessoryDO : accessoryDOList) {
            if (listMap.containsKey(cateringOrderItemAccessoryDO.getProductAccessoryId())) {
                accessoryList.add(cateringOrderItemAccessoryDO);
            }
        }
        return accessoryList;
    }

    public static List<CateringOrderItemAccessoryDO> accessoryDOInsertFilter(List<CateringOrderItemAccessoryDO> accessoryDOList,
                                                                             List<CateringOrderItemAccessoryDO> queryAccessory) {
        List<CateringOrderItemAccessoryDO> accessoryList = Lists.newLinkedList();
        Map<Long, List<CateringOrderItemAccessoryDO>> listMap = Maps.newHashMap();
        for (CateringOrderItemAccessoryDO cateringOrderItemAccessoryDO : queryAccessory) {
            if (listMap.containsKey(cateringOrderItemAccessoryDO.getProductAccessoryId())) {
                List<CateringOrderItemAccessoryDO> cateringOrderItemDOS = listMap.get(cateringOrderItemAccessoryDO.getProductAccessoryId());
                cateringOrderItemDOS.add(cateringOrderItemAccessoryDO);
            } else {
                List<CateringOrderItemAccessoryDO> cateringOrderItemDOS = Lists.newLinkedList();
                cateringOrderItemDOS.add(cateringOrderItemAccessoryDO);
                listMap.put(cateringOrderItemAccessoryDO.getProductAccessoryId(), cateringOrderItemDOS);
            }
        }
        for (CateringOrderItemAccessoryDO cateringOrderItemAccessoryDO : accessoryDOList) {
            if (!listMap.containsKey(cateringOrderItemAccessoryDO.getProductAccessoryId())) {
                accessoryList.add(cateringOrderItemAccessoryDO);
            }
        }
        return accessoryList;
    }
}
